/**
 * @description url参数处理
 */
const queryParams = {
  /**
   * @description 获取url参数
   * @param {string} url url地址
   * @returns {Record<string, string>} 参数对象
   */
  getQueryParams(url: string): Record<string, string> {
    const paramArr = url.split('?')[1].split('&');
    return paramArr.reduce((params: Record<string, string>, param: string) => {
      const [key, value] = param.split('=');
      params[key] = decodeURIComponent(value);
      return params;
    }, {});
  },

  /**
   * @description 添加url参数
   * @param {string} url url地址
   * @param {string} key 参数名
   * @param {string} value 参数值
   * @returns {string} 新的url
   */
  addQueryParam(url: string, key: string, value: string): string {
    const separator = url.includes('?') ? '&' : '?';
    return `${url}${separator}${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  },
};

export default queryParams;